//
//  PlaceDescription.m
//  Places
//
//  Created by alex on 9/21/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "PlaceDescriptionController.h"
#import "ViewControllerData.h"
#import "DataManagementSingleton.h"
#import "PlacesViewController.h"

#define textViewDeltaHeight		152

@implementation PlaceDescriptionController

#pragma mark 
#pragma mark properties

@synthesize titleField, subtitleField, descriptionField, backButton, editButton;
@synthesize annotation;

#pragma mark 
#pragma mark view did load

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	
	titleField.text = annotation.place.title;
	subtitleField.text = annotation.place.subtitle;
	descriptionField.text = annotation.place.descript;

	isEditing = YES;
	[self editAnnotation];
}

#pragma mark 
#pragma mark dealloc

- (void)dealloc {
	[titleField release];
	[subtitleField release];
	[descriptionField release];
	[backButton release];
	[editButton release];
	[annotation release];
    [super dealloc];
}

#pragma mark 
#pragma mark actions

- (IBAction)backToMap {
	[self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (IBAction)editAnnotation {
	isEditing = !isEditing;

	if (isEditing) {
		editButton.style = UIBarButtonItemStyleDone;
		[descriptionField becomeFirstResponder];
	}
	else {
		editButton.style = UIBarButtonItemStyleBordered;
		[descriptionField resignFirstResponder];
	}
	
	descriptionField.userInteractionEnabled = isEditing;
	backButton.enabled = !isEditing;	
}

- (IBAction)saveChanges {
	NSError *error;
	if (![[self managedObjectContext] save:&error]) {
		NSLog(@"Save error - %@", [error localizedDescription]);
	}
}

- (IBAction)deleteAnnotation {
	[[self managedObjectContext] deleteObject:annotation.place];

	NSError *error;
	if (![[self managedObjectContext] save:&error]) {
		NSLog(@"Deleting error - %@", [error localizedDescription]);
	}

	SEL removeAnnotation = @selector(removeAnnotation:);
	if ([self.parentViewController respondsToSelector:removeAnnotation]) {
		[self.parentViewController performSelector:removeAnnotation withObject:annotation];
	}

	[self backToMap];
}

#pragma mark 
#pragma mark text field touch;

- (void)textFieldTouch:(UITextField *)textField {
	if (!isEditing) {
		[self editAnnotation];
		[textField becomeFirstResponder];
	}
}

#pragma mark 
#pragma mark text field routine

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
	textField.borderStyle = UITextBorderStyleRoundedRect;
	return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
	textField.borderStyle = UITextBorderStyleNone;

	if (textField == titleField) {
		annotation.place.title = textField.text;
	}
	else if (textField == subtitleField) {
		annotation.place.subtitle = textField.text;
	}
}

#pragma mark 
#pragma mark text view routine

- (void)textViewDidBeginEditing:(UITextView *)textView {
	CGRect newRect = CGRectMake(textView.frame.origin.x, textView.frame.origin.y,
								textView.frame.size.width, textView.frame.size.height - textViewDeltaHeight);
	textView.frame = newRect;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
	CGRect newRect = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, 
								textView.frame.size.height + textViewDeltaHeight);
	textView.frame = newRect;
	
	annotation.place.descript = textView.text;
}

@end
